package com.itools.core.listener;

import com.itools.core.annotation.FileStrategy;
import com.itools.core.context.FileStrategyServiceContext;
import com.itools.core.em.StrategyType;
import com.itools.core.service.FileManagerService;
import com.itools.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @project: itools-backend
 * @description:
 * @author: XUCHANG
 * @create: 2021-04-01 16:06
 */
@Component
public class FileStrategyListener implements ApplicationListener<ContextRefreshedEvent> {
    @Autowired
    private FileStrategyServiceContext fileStrategyServiceContext;

    /**
     * 监听类
     * @param contextRefreshedEvent
     */
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {

        Map<String, FileManagerService> beans = contextRefreshedEvent.getApplicationContext().getBeansOfType(FileManagerService.class);
        beans.forEach((k, service) -> {
            String type = StrategyType.getType(k);
            fileStrategyServiceContext.put(type, service);
        });
    }
}
